package com.base.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.base.helper.BaiduMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.base.comm.CommContent;
import com.base.po.voip.SelfShop;
import com.base.po.voip.Users;
import com.base.service.LogsService;
import com.base.service.SelfShopService;
import com.base.service.UsersService;
import com.base.utils.IP;
import com.base.utils.Produce;

@Controller
@RequestMapping("/admin")
public class add_SelfShop {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private SelfShopService selfShopService;
	@Autowired
	private UsersService usersService;
	String SUCCESS = "success";

	// 申请自建商城店铺
	@RequestMapping(value = "/insertSelfShop", method = RequestMethod.POST)
	public void insertSelfShop(HttpServletRequest request, HttpServletResponse response,
			String shopname, String shoplogo, String shopbg, String address, String contacts, 
			Double longitude, Double latitude, String busintime, String discount, String phonenum,
			String introduction, String attestation1, String attestation2, String attestation3,
			String attestation4) throws IOException {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}	
		Users users = usersService.selectByPrimaryKey(adminId);

		Map<String, Object> jsonMap = new HashMap<>();
		String city = null;
		String district = null;
		try {
			jsonMap = BaiduMapHelper.getCity(request, longitude, latitude);
			city = (String)jsonMap.get("city");
			district = (String)jsonMap.get("district");
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (attestation1 == "" || attestation1 == "undefined") {
			attestation1 = null;
		}
		if (attestation2 == "" || attestation2 == "undefined") {
			attestation2 = null;
		}
		if (attestation3 == "" || attestation3 == "undefined") {
			attestation3 = null;
		}
		if (attestation4 == "" || attestation4 == "undefined") {
			attestation4 = null;
		}

		String id = Produce.getUUID(); 
		int resultCode = 0;
		if(users.getRole() == 2) {
			resultCode = selfShopService.insertSelective(new SelfShop(id, users.getParentid(), adminId,
					Produce.getNowDate(), shopname, shoplogo, shopbg, address, city, district, latitude,
					longitude, contacts, phonenum, introduction, busintime, discount, (byte)1, (byte)2,
					attestation1, attestation2, attestation3, attestation4));
		}
		
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		if (resultCode == 1) {
			logsService.insert(CommContent.TYPE_UPDATE, shopname+"自营商城申请入驻提交成功", usernamesys,
					true, IP.getIpAddr(request));
			out.print(SUCCESS);
		} else {
			logsService.insert(CommContent.TYPE_UPDATE, shopname+"自营商城申请入驻提交失败", usernamesys,
					true, IP.getIpAddr(request));
			out.print("更改失败，请您重试");
		}
	}

}
